<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="rar.constants.html">« 预定义常量</a></li>
      <li style="float: right;"><a href="ref.rar.html">Rar 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.rar.html">Rar</a></li>
    <li>范例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="rar.examples" class="chapter">
 <h1>范例</h1>

 
 <p class="para">
  See also the examples under <a href="wrappers.rar.html" class="link"><code class="literal">rar://</code> wrapper</a>.
 </p>
 
 <p class="para">
  <div class="example" id="example-793">
   <p><strong>示例 #1 On-the-fly decompression</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">"i"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">)&nbsp;||&nbsp;!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">"i"</span><span style="color: #007700">]))<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Index&nbsp;unspecified&nbsp;or&nbsp;non-numeric"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$index&nbsp;</span><span style="color: #007700">=&nbsp;(int)&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">"i"</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">$arch&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">RarArchive</span><span style="color: #007700">::</span><span style="color: #0000BB">open</span><span style="color: #007700">(</span><span style="color: #DD0000">"example.rar"</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$arch&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Cannot&nbsp;open&nbsp;example.rar"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$arch</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getEntries</span><span style="color: #007700">();<br />if&nbsp;(</span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Cannot&nbsp;retrieve&nbsp;entries"</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$index</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$entries</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"No&nbsp;such&nbsp;index:&nbsp;</span><span style="color: #0000BB">$index</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$orfilename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$entries</span><span style="color: #007700">[</span><span style="color: #0000BB">$index</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//UTF-8&nbsp;encoded<br /><br /></span><span style="color: #0000BB">$filesize&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$entries</span><span style="color: #007700">[</span><span style="color: #0000BB">$index</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getUnpackedSize</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;you&nbsp;could&nbsp;check&nbsp;HTTP_IF_MODIFIED_SINCE&nbsp;here&nbsp;and&nbsp;compare&nbsp;with<br />&nbsp;*&nbsp;$entries[$index]-&gt;getFileTime().&nbsp;You&nbsp;could&nbsp;also&nbsp;send&nbsp;a<br />&nbsp;*&nbsp;"Last&nbsp;modified"&nbsp;header&nbsp;*/<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$entries</span><span style="color: #007700">[</span><span style="color: #0000BB">$index</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">getStream</span><span style="color: #007700">();<br />if&nbsp;(</span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Cannot&nbsp;open&nbsp;file&nbsp;with&nbsp;index&nbsp;</span><span style="color: #0000BB">$index</span><span style="color: #DD0000">&nbsp;insided&nbsp;the&nbsp;archive."</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$arch</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//no&nbsp;longer&nbsp;needed;&nbsp;stream&nbsp;is&nbsp;independent<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">detectUserAgent</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">array_key_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"Other"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$uas&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'HTTP_USER_AGENT'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"@Opera/@"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$uas</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"Opera"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"@Firefox/@"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$uas</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"Firefox"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"@Chrome/@"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$uas</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"Chrome"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #DD0000">"@MSIE&nbsp;([0-9.]+);@"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$uas</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$matches</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(((float)</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">])&nbsp;&gt;=&nbsp;</span><span style="color: #0000BB">7.0</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"IE"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"Other"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;We&nbsp;have&nbsp;3&nbsp;options:<br />&nbsp;*&nbsp;-&nbsp;For&nbsp;FF&nbsp;and&nbsp;Opera,&nbsp;which&nbsp;support&nbsp;RFC&nbsp;2231,&nbsp;use&nbsp;that&nbsp;format.<br />&nbsp;*&nbsp;-&nbsp;For&nbsp;IE&nbsp;and&nbsp;Chrome,&nbsp;use&nbsp;attwithfnrawpctenclong<br />&nbsp;*&nbsp;&nbsp;&nbsp;(http://greenbytes.de/tech/tc2231/#attwithfnrawpctenclong)<br />&nbsp;*&nbsp;-&nbsp;For&nbsp;the&nbsp;others,&nbsp;convert&nbsp;to&nbsp;ISO-8859-1,&nbsp;if&nbsp;possible<br />&nbsp;*/<br /></span><span style="color: #0000BB">$formatRFC2231&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Content-Disposition:&nbsp;attachment;&nbsp;filename*=UTF-8\'\'%s'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$formatDef&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Content-Disposition:&nbsp;attachment;&nbsp;filename="%s"'</span><span style="color: #007700">;<br /><br />switch&nbsp;(</span><span style="color: #0000BB">detectUserAgent</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #DD0000">"Opera"</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #DD0000">"Firefox"</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$orfilename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rawurlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$orfilename</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$formatRFC2231</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #DD0000">"IE"</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #DD0000">"Chrome"</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$orfilename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rawurlencode</span><span style="color: #007700">(</span><span style="color: #0000BB">$orfilename</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$formatDef</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">function_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'iconv'</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$orfilename&nbsp;</span><span style="color: #007700">=<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@</span><span style="color: #0000BB">iconv</span><span style="color: #007700">(</span><span style="color: #DD0000">"UTF-8"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"ISO-8859-1//TRANSLIT"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$orfilename</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$formatDef</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$orfilename</span><span style="color: #007700">));<br /></span><span style="color: #FF8000">//cannot&nbsp;send&nbsp;error&nbsp;messages&nbsp;from&nbsp;now&nbsp;on&nbsp;(headers&nbsp;already&nbsp;sent)<br /><br />//replace&nbsp;by&nbsp;real&nbsp;content&nbsp;type,&nbsp;perhaps&nbsp;infering&nbsp;from&nbsp;the&nbsp;file&nbsp;extension<br /></span><span style="color: #0000BB">$contentType&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"application/octet-stream"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Content-Type:&nbsp;</span><span style="color: #0000BB">$contentType</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Content-Transfer-Encoding:&nbsp;binary"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">"Content-Length:&nbsp;</span><span style="color: #0000BB">$filesize</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'REQUEST_METHOD'</span><span style="color: #007700">]&nbsp;==&nbsp;</span><span style="color: #DD0000">"HEAD"</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die();<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;@</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8192</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;</span><span style="color: #FF8000">//useless&nbsp;to&nbsp;send&nbsp;error&nbsp;messages<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="para">
  This example opens a RAR file and presents the requested file inside the RAR archive for download to the client.
 </p>
 
 <p class="para">
  <div class="example" id="example-794">
   <p><strong>示例 #2 RAR extension filesystem extraction example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$rar_file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rar_open</span><span style="color: #007700">(</span><span style="color: #DD0000">'example.rar'</span><span style="color: #007700">)&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Can't&nbsp;open&nbsp;Rar&nbsp;archive"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">rar_list</span><span style="color: #007700">(</span><span style="color: #0000BB">$rar_file</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$entries&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$entry</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Filename:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$entry</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Packed&nbsp;size:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$entry</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getPackedSize</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Unpacked&nbsp;size:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$entry</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUnpackedSize</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$entry</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">extract</span><span style="color: #007700">(</span><span style="color: #DD0000">'/dir/extract/to/'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">rar_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$rar_file</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="para">
  This example opens a RAR file archive and extracts each entry to the
  specified directory.
 </p>
 
</div>
</div></div></body></html>